package com.tensquare.user.service;

import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * Created by WF on 2020/4/3 14:53
 */
@Service
public class AdminService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private BCryptPasswordEncoder encoder;
    //1.添加管理员
    public void add(Admin admin) {
        //1.1)设置主键
        admin.setId(idWorker.nextId() + "");
        //加密密码
        admin.setPassword(encoder.encode(admin.getPassword()));
        //1.2)保存
        adminDao.save(admin);
    }
    //2.管理员登录
    public Admin login(Admin admin) {
        //2.1)根据登录名得到管理员用户
        Admin adminUser = adminDao.findByLoginname(admin.getLoginname());
        //2.2)判断是否存在
        if(adminUser != null && encoder.matches(admin.getPassword(),adminUser.getPassword())){
            return adminUser;
        }
        return null;
    }
    //3.删除管理员
    public void delete(String adminId) {
        adminDao.deleteById(adminId);
    }
}
